package net.monare.mifss.admin.ui.wiz;

import net.monare.mifss.MifssMessages;
import net.monare.mifss.evn.StorageCardListener;

import com.vaadin.server.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.Runo;

public class BaseWizard extends Window {

	private static final long serialVersionUID = 7417115994930070333L;

	private Panel cardPanel;
	
	private VerticalLayout mainLayout;
	private GridLayout bottomBand;
	
	private StorageCardListener storageCardListener;
	private NativeButton nextButton;
	private NativeButton backButton;
	private Component[] cards;
	
	
	public void setCards(Component[] cards) {
		
		this.cards = cards;
		
		storageCardListener = new StorageCardListener(this, cards);
		nextButton.addClickListener(storageCardListener);
		backButton.addClickListener(storageCardListener);
		
		setCard(cards[0]);

	}

	public BaseWizard(String title) {

		setSizeFull();

		cardPanel = new Panel();
		cardPanel.addStyleName(Runo.PANEL_LIGHT);

		cardPanel.setHeight("500px");
		cardPanel.setWidth("700px");

		mainLayout = new VerticalLayout();
		
		
		// init control buttons
		nextButton = new NativeButton(MifssMessages.getMessage("button.next"));
		nextButton.setIcon(new ThemeResource("img/arrow_right.png"));
		nextButton.setId("NEXT");

		backButton = new NativeButton(MifssMessages.getMessage("button.back"));
		backButton.setIcon(new ThemeResource("img/arrow_left.png"));
		backButton.setId("BACK");

		bottomBand = new GridLayout(2,1);
		bottomBand.setSpacing(true);
		bottomBand.setMargin(true);
		bottomBand.setSizeFull();
		
		//
		// setup window
		setCaption(title);
		setStyleName(Runo.WINDOW_DIALOG);

		setHeight("600px");
		setWidth("725px");
		setModal(true);

		mainLayout.addComponent(cardPanel);
		mainLayout.addComponent(bottomBand);
		mainLayout.setComponentAlignment(bottomBand, Alignment.BOTTOM_CENTER);

		setContent(mainLayout);

	}

	public void setCard(Component component) {
		
		cardPanel.setContent(component);
		
		int pos = 0;
		
		bottomBand.removeAllComponents();
		
		// determine card position
		for(int i=0; i<cards.length; i++){
			if(component.getId().equals(cards[i].getId())){
				pos = i;
				break;
			}
		}
		
		if(pos == 0){
			
			bottomBand.addComponent(nextButton, 1, 0);
			bottomBand.setComponentAlignment(nextButton, Alignment.BOTTOM_RIGHT);
			
		} else if(pos == cards.length -1){
			
			bottomBand.addComponent(backButton, 0, 0);
			bottomBand.setComponentAlignment(backButton, Alignment.BOTTOM_LEFT);
			
		} else {
			
			bottomBand.addComponent(backButton, 0, 0);
			bottomBand.addComponent(nextButton, 1, 0);

			bottomBand.setComponentAlignment(backButton, Alignment.BOTTOM_LEFT);
			bottomBand.setComponentAlignment(nextButton, Alignment.BOTTOM_RIGHT);
			
		}
				
	}

	public String getCardId() {
		return cardPanel.getContent().getId();
	}

}
